package com.collectplus.express.parcel;

import android.content.Intent;
import android.os.Handler;
import android.os.Message;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.EditText;
import android.widget.ListView;

import com.collectplus.express.BaseActivity2;
import com.collectplus.express.R;
import com.collectplus.express.logic.AppHttp;
import com.collectplus.express.logic.AppLogic;
import com.collectplus.express.logic.AppResult;
import com.collectplus.express.logic.JSON;
import com.collectplus.express.logic.MsgID;
import com.collectplus.express.model.ParcelDetailBeanNew;
import com.collectplus.express.model.ParcelSendBean;
import com.collectplus.express.model.PassportDetailBean;

import java.util.ArrayList;

import droid.frame.utils.lang.Str;

/**
 * 包裹搜索
 *
 * @author kaka <br/>
 *         2017年2月28日
 */
public class ParcelSearchActivity extends BaseActivity2 implements OnClickListener {
    private ParcelSendAdapter adapter;
    private ListView mListView;
    private EditText mSearchEdit;
    private ArrayList<ParcelSendBean> parcels;
    private int clickPosition;

    @Override
    protected void findViewById() {
        setContentView(R.layout.parcel_search);
        super.findViewById();
        findViewById(R.id.search_cancel).setOnClickListener(this);
        adapter = new ParcelSendAdapter(null, this, this);
        // listView设置item点击
        mListView = (ListView) findViewById(R.id.list_view);
        mListView.setAdapter(adapter);
        mListView.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                clickPosition = position;
                ParcelSendBean parcel = parcels.get(position);
                int isCover = 0;
                if (!Str.isEmpty(parcel.getOrderId2())) {
                    isCover = 1;
                }
                showLoadingDialog("");
                AppHttp.getInstance().getParcelDetailNew(parcel.getParcelId(), isCover);
            }
        });
        // 搜索框设置监听
        mSearchEdit = (EditText) findViewById(R.id.search_edit);
        mSearchEdit.setFocusable(true);
        mSearchEdit.setFocusableInTouchMode(true);
        mSearchEdit.requestFocus();
        mSearchEdit.addTextChangedListener(new TextWatcher() {
            private Handler handler = new Handler() {
                @Override
                public void handleMessage(Message msg) {
                    if (msg.what == 9000) {
                        String searchKey = mSearchEdit.getText().toString();
                        if (!Str.isEmpty(searchKey)) {
                            AppHttp.getInstance().getParcelSearchList(searchKey);
                        }
                    }
                }
            };

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
            }

            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
            }

            @Override
            public void afterTextChanged(Editable s) {
                handler.removeMessages(9000);
                handler.sendEmptyMessageDelayed(9000, 500);
            }
        });
    }

    @Override
    public boolean handleMessage(Message msg) {
        switch (msg.what) {
            case MsgID.passport_detail:
                final AppResult<PassportDetailBean> result1 = JSON.parser(msg.obj, PassportDetailBean.class);
                cancelLoadingDialog();
                if (result1.isSuccess()) {
                    PassportDetailBean passport = result1.getResult();
                    if (passport != null) {
                        AppLogic.getInstance().showPassportDialog(this, passport, null);
                    }
                } else {
                    showToast(result1.getMessage());
                }
                return false;
            case MsgID.parcel_detail:
                cancelLoadingDialog();
                try {
                    final AppResult<ParcelDetailBeanNew> result = JSON.parser(msg.obj, ParcelDetailBeanNew.class);// 解析可能出现问题
                    if (result.isSuccess()) {
                        ParcelDetailBeanNew parcelDetail = result.getResult();
                        // 避免混乱在后台接到的消息不多展示处理
                        if (parcelDetail != null) {
                            Intent intent = new Intent(this, ParcelDetailActivity.class);
                            ParcelSendBean sendBean = parcels.get(clickPosition);
                            intent.putExtra("parcelStatus", sendBean.getParcelStatus());
                            intent.putExtra("orderStatus", sendBean.getOrderStatus());
                            intent.putExtra("parcelDetailBean", parcelDetail);
                            startActivity(intent);
                        }
                    } else {
                        // 未知原因是否需要显示
                        showToast(result.getMessage());
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
                return true;
            case MsgID.parcel_list_search:
                final AppResult<ParcelSendBean> result = JSON.parseList(msg.obj, ParcelSendBean.class);
                if (result.isSuccess()) {
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            parcels = result.getResultList();
                            if (parcels != null) {
                                adapter.setItems(parcels, true);
                            }
                        }
                    });
                } else {
                    showToast(result.getMessage());
                }
                return true;
            default:
                return super.handleMessage(msg);
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.search_cancel:
                finish();
                break;
            case R.id.parcel_status_operate:
                String parcelId = (String) v.getTag();
                showLoadingDialog("");
                AppHttp.getInstance().getPassportDetail(parcelId, 0);
                //			AppLogic.getInstance().showPassportDialog(getContext(),parcelId,null,0);
                break;
            default:
                break;
        }
    }

}
